package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.ConceptoInicial;

public class ConceptoInicialDAOImpl extends JdbcDaoSupport implements IConceptoInicialDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;
    
    private FindByRadicado findByRadicado;
    
    private FindByIdSolicitud findByIdSolicitud;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM GES_CONCEPTO_INICIAL_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(ConceptoInicial conceptoInicial) {
		deleteOperation.execute( conceptoInicial  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public ConceptoInicial findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(ConceptoInicial conceptoInicial) {
		insertOperation.execute( conceptoInicial );
	}

	public void update(ConceptoInicial conceptoInicial) {
		updateOperation.execute( conceptoInicial );
	}

	public ConceptoInicial findByCriteria(ConceptoInicial  conceptoInicial) {
		return findByCriteriaQuery.runQuery(conceptoInicial);
	}

	public List load() {
		return findAllQuery.runQuery();
	}

	public List loadByRadicado(ConceptoInicial conceptoInicial) {
		return findByRadicado.runQuery(conceptoInicial);
	}
	
	public List loadByIdSolicitud(ConceptoInicial conceptoInicial) {
		return findByIdSolicitud.runQuery(conceptoInicial);
	}
	
	public ConceptoInicial findByActiveState(ConceptoInicial  conceptoInicial) {
		return findByActiveStateQuery.runQuery(conceptoInicial);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    //	deleteOperation = new DeleteOperation(  getDataSource() );
    //	deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    //	findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findByIdSolicitud = new FindByIdSolicitud(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	findByRadicado = new FindByRadicado(getDataSource());
    //	findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO GES_CONCEPTO_INICIAL_T ("
				  + " ID, "
				  + " ID_SOLICITUD, "
				  + " ID_CONTRAVENCION, "
				  + " DESCRIPCION, "
				  + " OBSERVACION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " GES_CONCEPTO_INICIAL_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(ConceptoInicial conceptoInicial) {			
			InsertOperation.this.update( new Object[]{
				conceptoInicial.getIdSolicitud(),
				conceptoInicial.getIdContravencion(),
				conceptoInicial.getDescripcion(),
				conceptoInicial.getObservacion(),
				conceptoInicial.getEstado(),
				conceptoInicial.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE GES_CONCEPTO_INICIAL_T SET "
					  + " ID_SOLICITUD = ? ,"
					  + " ID_CONTRAVENCION = ? ,"
					  + " DESCRIPCION = ? ,"
					  + " OBSERVACION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE "
				  	  + WHERE
			  	  	  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(ConceptoInicial conceptoInicial) {
			UpdateOperation.this.update( new Object[]{				
				conceptoInicial.getIdSolicitud(),
				conceptoInicial.getIdContravencion(),
				conceptoInicial.getDescripcion(),
				conceptoInicial.getObservacion(),
				conceptoInicial.getEstado(),			
				conceptoInicial.getUsuarioModifica(),				
				conceptoInicial.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_CONCEPTO_INICIAL_T "
										+ WHERE + " 1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(ConceptoInicial conceptoInicial) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM GES_CONCEPTO_INICIAL_T "
										+ WHERE
			  	  	  					+ " ID = ? ";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
					new Long(id)
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ConceptoInicial conceptoInicial = new ConceptoInicial();
				conceptoInicial.setId(new Long(rs.getLong("ID")));
				conceptoInicial.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
				conceptoInicial.setIdContravencion(new Long(rs.getLong("ID_CONTRAVENCION")));
				conceptoInicial.setIdContravencionStr(rs.getString("ID_CONTRAVENCION"));
				conceptoInicial.setDescripcion(rs.getString("DESCRIPCION"));
				conceptoInicial.setObservacion(rs.getString("OBSERVACION"));
				conceptoInicial.setEstado(rs.getString("ESTADO"));
				conceptoInicial.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				conceptoInicial.setFechaCrea(rs.getDate("FECHA_CREA"));		
				conceptoInicial.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				conceptoInicial.setFechaModifica(rs.getDate("FECHA_MODIFICA"));	
				conceptoInicial.setStrContravencion(rs.getString("VRT_NOMBRE"));	
			return conceptoInicial;
		}
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperationCriteria extends MappingSqlQuery {
		public BaseQueryOperationCriteria(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			ConceptoInicial conceptoInicial = new ConceptoInicial();
			conceptoInicial.setId(new Long(rs.getLong("ID")));
			conceptoInicial.setIdSolicitud(new Long(rs.getLong("ID_SOLICITUD")));
			conceptoInicial.setIdContravencion(new Long(rs.getLong("ID_CONTRAVENCION")));
			conceptoInicial.setDescripcion(rs.getString("DESCRIPCION"));
			conceptoInicial.setObservacion(rs.getString("OBSERVACION"));
			conceptoInicial.setEstado(rs.getString("ESTADO"));
			conceptoInicial.setUsuarioCrea(rs.getString("USUARIO_CREA"));
			conceptoInicial.setFechaCrea(rs.getDate("FECHA_CREA"));
			conceptoInicial.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
			conceptoInicial.setFechaModifica(rs.getDate("FECHA_MODIFICA"));
			return conceptoInicial;
		}
    }

	// ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE
			  	  	  					+ " ID = ? ";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public ConceptoInicial runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (ConceptoInicial) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }
    
    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByRadicado extends BaseQueryOperation {
		private static final String sql = "SELECT CONC.*,MAE.VRT_NOMBRE from GES_CONCEPTO_INICIAL_T CONC INNER JOIN "
										 + " ADM_VALOR_TIPO_MAESTRA_T MAE ON CONC.ID_CONTRAVENCION = MAE.ID_VALOR_TIPO "
										 + WHERE  +" ID_SOLICITUD IN " +
										"(SELECT ID FROM " +
										"ADM_SOLICITUD_T WHERE RADICADO LIKE ? ) ";
    	public FindByRadicado(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.CHAR) );
			compile();
    	}
    	public List runQuery(ConceptoInicial conceptoInicial) {
			List result =  execute( new Object[]{
					"%"+ conceptoInicial.getRadicado() + "%"
				});
			if ( result.size() > 0 ) {
        		return result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperationCriteria {
		private static final String sql = SELECT
										+ WHERE + " 1=1 "
			  	  	  					+ " AND ID_SOLICITUD = ? ";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
			declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public ConceptoInicial runQuery(ConceptoInicial conceptoInicial) {
			List result =  execute( new Object[]{
				conceptoInicial.getIdSolicitud()
				});
			if ( result.size() > 0 ) {
        		return (ConceptoInicial) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public List runQuery() {
			List result =  execute();
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public ConceptoInicial runQuery(ConceptoInicial conceptoInicial) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (ConceptoInicial) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    class FindByIdSolicitud extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " ID_SOLICITUD=? ";
    	public FindByIdSolicitud(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public List runQuery(ConceptoInicial conceptoInicial) {
			List result =  execute( new Object[]{
				conceptoInicial.getIdSolicitud()
				});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

}
